/**
 * 
 */
package cn.xiaolang.function.switchVsIf;

public class Demo {

	public static void main(String[] args) throws InterruptedException {
		SendType type = SendType.EMAIL;
		long startOne = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			switchTest(type);
		}
		long endOne = System.currentTimeMillis();
		System.err.println(endOne - startOne);
		System.gc();
		long startTow = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			ifTest(type);
		}
		long endTow = System.currentTimeMillis();
		System.err.println(endTow - startTow);
	}

	/**
	 * switch case 只需要一次读取到寄存器中;适用于分支比较的情况->效率高于if else
	 * 
	 */
	public static void switchTest(SendType type) throws InterruptedException {
		switch (type) {
		case EMAIL:
			Thread.sleep(1);
			break;
		case PHONE:
			Thread.sleep(1);
			break;
		case MOBILE:
			Thread.sleep(1);
			break;
		default:
			break;
		}
	}

	/**
	 * if 在每次判断的时候，都需要重新把type的值，读取到寄存器中
	 */
	public static void ifTest(SendType type) throws InterruptedException {
		if (type.equals(SendType.EMAIL)) {
			Thread.sleep(1);
		} else if (type.equals(SendType.PHONE)) {
			Thread.sleep(1);
		} else if (type.equals(SendType.MOBILE)) {
			Thread.sleep(1);
		}
	}
}


enum SendType {
	
	MOBILE,
	
	EMAIL,
	
	PHONE;
}
